<? 
/**
 * Handles retrival and display of artist information from Last.fm
 * 
 * @author Magnus Stoveland <mstoveland@gmail.com>
 */
class ArtistWiki 
{
    /**
     * Gets the biography and additional information for an artist on lastfm
     * Inspired by http://www.last.fm/forum/21604/_/310550
     *
     * @param string $artist
     * @return XML data with artist info
     */
    function getArtistMetadataXML($artist) {
        $artist = ArtistWiki::encode_temp($artist);
        echo $artist;
        $data = '<?xml version="1.0" encoding="UTF-8"?'.'>
        <methodCall>
        <methodName>artistMetadata</methodName>
        <params>
        <param><value><string>'.htmlentities($artist).'</string></value></param>
        <param><value><string>en</string></value></param>
        </params>
        </methodCall>';
        $host = 'ws.audioscrobbler.com';
        $uri = 'http://ws.audioscrobbler.com/1.0/rw/xmlrpc.php';
        if (($fp = @fsockopen($host, 80, $errno, $errstr, 30)) !== false) {
            $header = "POST $uri HTTP/1.0\r\n";
            $header .= "Host: $host\r\n";
            $header .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n";
            $header .= "Content-type: text/xml;charset=utf-8\r\n";
            $header .= "Content-length: " . strlen($data) . "\r\n";
            $header .= "Connection: close\r\n\r\n";            
            fputs($fp,$header);
            fputs($fp,$data);
            $buf = '';
            while (!feof($fp)) {
                $buf .= fgets($fp,1024);
            }
            fclose($fp);
        }        
        // Strip away meta request-data
        $temparr = explode('<?xml version="1.0" encoding="UTF-8"?>', $buf);
        $outxml = '<?xml version="1.0" encoding="UTF-8"?>'.$temparr[1];
        return $outxml;
    }
    
    /**
     * Strip any "BBcode" from a string, taken from http://twelvestone.com/forum_thread/view/28916 
     *
     * @param string $text_to_search
     * @return unknown
     */
    function stripBBCode($text_to_search) {
        $pattern = '|[[\/\!]*?[^\[\]]*?]|si';
        $replace = '';
        return preg_replace($pattern, $replace, $text_to_search);
    }
    
    function encode_temp($string) {
        $charTable = array(
            "å" => "\303\245",
            "ö" => "\303\266"
        );    
        foreach($charTable as $orig => $replace) {
            echo $orig." - ".$replace;
            $string = str_replace($orig, $replace, $string);
        }
        return $string;
    }
}
?>